//
//  HomeViewController.swift
//  account
//
//  Created by Qingke on 16/4/26.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {
    
    var dataModel: DataModel?
    
    var todayAccount: Double?
    
    var monthIncome: Double?{
        didSet{
            monthIncomeLabel.text = String(monthIncome!)
        }
    }
    var monthOutlay: Double?{
        didSet{
            monthOutlayLabel.text = String(monthOutlay)
        }
    }
    var cha: Double? {
        didSet{
            chaLabel.text = String(cha)
        }
    }
    
    @IBOutlet weak var monthOutlayLabel: UILabel!
    @IBOutlet weak var monthIncomeLabel: UILabel!
    @IBOutlet weak var chaLabel: UILabel!
     
    @IBOutlet weak var todayLabel: UILabel!
        @IBOutlet weak var mytableView: UITableView!
        override func viewDidLoad() {
        super.viewDidLoad()
        //为页面中的列表提供支持
        mytableView.delegate = self
        mytableView.dataSource = self
        // 获取app代理内的数据
        let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        dataModel = appDel.dataModel
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        shuju()
        mytableView.reloadData()
    }
    
    func shuju() {
        
        let income = dataModel?.income
        let outlay = dataModel?.outlay
        monthOutlay = dataModel?.monthOutlay
        monthIncome = dataModel?.monthIncome
        cha = dataModel?.monthCha
        todayAccount = income! - outlay!
    }
    

}

extension HomeViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCellWithIdentifier("todayCell", forIndexPath: indexPath) as! myViewCell
        if indexPath.row == 0{
            
            cell.nameLabel.text = "今日差额："
            cell.numberLabel.text = String(todayAccount!)
        } else {
            cell.nameLabel.text = "昨日差额："
            cell.numberLabel.text = String(todayAccount!)
        }
        return cell
    }
        

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
        }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return CGFloat(40)
    }
    
}
extension HomeViewController : UITableViewDelegate {
    
}

